<?php

namespace app\proxy\controller;

use app\data\model\LiveAppList;
use app\data\model\LiveProxyList;
use Ip2Region;
use think\admin\Controller;
/**
 * 贴牌代理列表
 * Class ProxyListController
 * @package app\proxy\controller
 */
class ProxyListController extends Controller
{
    /**
     * 查询
     * @auth true  # 表示需要验证权限
     * @menu true  # 添加系统菜单节点
     * @login true # 强制登录才可访问
     */
    public function index(){
        $this->title = '贴牌代理列表';
        $this->type = $this->request->get('type', 'index');
        $this->proxys = LiveProxyList::mk()->field('id,user_nickname,user_account')->select()->toArray();
        $this->apps = LiveAppList::mk()->field('app_id,app_name')->select()->toArray();

        $query = LiveProxyList::mQuery();
        if ($this->type === 'index') $query->where(['deleted' => 0]);
        else $this->error("无法加载 {$this->type} 数据列表！");
        $query->like('user_nickname');
        $query->equal('pid,user_state,user_autotype');
        $query->dateBetween('create_time');
        $query->order('create_time desc')->page();
    }
    /**
     * 列表数据处理
     * @auth true
     * @param array $data
     * @throws \Exception
     */
    protected function _index_page_filter(array &$data)
    {
        $region = new Ip2Region();
        foreach ($data as &$vo) {
            $isp = $region->btreeSearch($vo['login_ip']);
            $vo['login_address'] = str_replace(['内网IP', '0', '|'], '', $isp['region'] ?? '') ?: '-';
        }
    }

    /**
     * 设置登录状态
     * @auth true  # 表示需要验证权限
     * @menu true  # 添加系统菜单节点
     * @login true # 强制登录才可访问
     */
    public function userstate()
    {
        LiveProxyList::mSave($this->_vali([
            'user_state.in:0,1'  => '状态值范围异常！',
            'user_state.require' => '状态值不能为空！',
        ]), 'id');
    }
}